using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
#if UNITY_2022_2_OR_NEWER
using UnityEngine.XR.Interaction.Toolkit;
#endif
using static VRig;
public class VTouchInteractNode : MonoBehaviour
{
    public VTouchManager manager;
    public RigIKUnit IKUnit;


#if UNITY_2022_2_OR_NEWER
    public VTouchGrabInteractable touchInteractable; //接口做不了序列化， 相关的在Editor环境都干不了
#else
    public VTouchColliderInteractable touchInteractable; 
#endif
    



    public bool inInteracting = false;
    private void OnTriggerEnter(Collider other)
    {
        //如果是grab主导的交互，那么这个enter几乎不生效
    }

    public void OnRegisteToManager(bool isGrab = true)
    {
        if (isGrab)
        {
            var vTouchGrab = touchInteractable;
            var grab = vTouchGrab.interactable;
            //这也只是一个交互入口
//             grab.hoverEntered.AddListener((args) =>
//             {
//                 //print("你摸我干什么！！");
//             });
// 
//             //核心的设置权重代码
//             grab.selectEntered.AddListener((args) =>
//             {
//                 IKUnit.SetWeight(1);
//                 inInteracting = true;
//             });
// 
//             grab.selectExited.AddListener((args) =>
//             {
//                 new TObserver().Tween(1, 0, 1, x => IKUnit.SetWeight(x));
//                 inInteracting = false;
//             });
        }
    }

    private void FixedUpdate()
    {
        if (!inInteracting)
        {
            IKUnit.goal.transform.position = transform.position;
            IKUnit.goal.transform.rotation = transform.rotation;
        }
    }

    public void OnMouseRayEnter()
    {
        print(IKUnit);
        print(manager.frame);
        OnReport();
    }

    public void OnAdd(VTouchManager manager, RigIKUnit rigIKUnit, bool isXRNode = false)
    {
        this.manager = manager;
        IKUnit = rigIKUnit;
        if (isXRNode)
        {
            // touchInteractable = new VTouchGrabInteractable();
            // touchInteractable.Load(IKUnit.goal);
        }
        else
        {
            //touchInteractable = new VTouchColliderInteractable();
            //touchInteractable.Load(IKUnit.goal);
        }
    }
    public void OnRemove()
    {
        touchInteractable.Unload();

        this.manager = null;
        gameObject.RemoveComponent<VTouchInteractNode>();

    }

    public void OnReport()
    {
        manager.activingNodes.Push(IKUnit);
    }
#if UNITY_2022_2_OR_NEWER
    [Serializable]
    public class VTouchSimpleInteractable : VTouchInteractablebase
    {
        public XRSimpleInteractable interactable;

        public override Vector3 GetCenter() => Vector3.zero;

        public override void Load(Transform nodeTransform)
        {
            this.nodeTransform = nodeTransform;
            interactable = nodeTransform.gameObject.UniqueComponent<XRSimpleInteractable>();
        }

        public override void SetActive(bool active)
        {
            interactable.enabled = active;
        }

        public override void Unload()
        {

        }
    }
    [Serializable]
    public class VTouchGrabInteractable : VTouchInteractablebase
    {
        public XRGrabInteractable interactable;
        public SphereCollider sphereCollider;
        public override Vector3 GetCenter() => Vector3.zero;

        public override void Load(Transform nodeTransform)
        {
            this.nodeTransform = nodeTransform;
            interactable = nodeTransform.gameObject.UniqueComponent<XRGrabInteractable>();
            sphereCollider = nodeTransform.gameObject.UniqueComponent<SphereCollider>();
            var rigi = nodeTransform.gameObject.UniqueComponent<Rigidbody>();
            rigi.useGravity = false;
            sphereCollider.radius = 0.1f;
        }

        public override void SetActive(bool active)
        {
            interactable.enabled = active;
        }

        public override void Unload()
        {
            nodeTransform.gameObject.RemoveComponent<XRGrabInteractable>();
            nodeTransform.gameObject.RemoveComponent<Rigidbody>();
            nodeTransform.gameObject.RemoveComponent<SphereCollider>();
        }
    }
#endif
    [Serializable]
    public class VTouchColliderInteractable : VTouchInteractablebase
    {
        public SphereCollider interactable;
        public override void SetActive(bool active)
        {
            interactable.enabled = active;
        }
        public override void Load(Transform nodeTransform)
        {
            this.nodeTransform = nodeTransform;
            interactable = nodeTransform.gameObject.UniqueComponent<SphereCollider>();
            interactable.radius = 0.1f;
        }

        public override void Unload()
        {

        }

        public override Vector3 GetCenter() => interactable.center;
    }


}




